package online.shenme.day13;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class ProcessTest {
    public static void main(String[] args) throws Exception {
       ProcessImpl prc= new ProcessImpl("test");
        System.out.println(prc.getName());
        prc.start();
        System.out.println(prc.get());
//        new Thread(()->{
//            for (int i = 0; i < 50; i++) {
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(Thread.currentThread().getName()+"多线程"+i);
//            }
//        },"132").start();
//        new Thread(()->{
//            for (int i = 0; i < 50; i++) {
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(Thread.currentThread().getName()+"多线程"+i);
//            }
//        },"321").start();
        new Thread(new FutureTask<Integer>(()->{
            int sum=0;
            for (int i = 0; i < 50; i++) {
                sum +=i;
            }
            return sum;
        })).start();
    }
}

class ProcessImpl extends Thread implements Runnable, Callable<Integer> {
    public ProcessImpl() {
    }

    public ProcessImpl(String name) {
        super(name);
    }

    @Override
    public void run(){
        for (int i = 0; i < 50; i++) {
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }

    @Override
    public Integer call() throws Exception {
        int sum=0;
            for (int i = 0; i < 50; i++) {
                sum +=i;
            }
        System.out.println(Thread.currentThread().getName());
            return sum;
    }
    public int get() throws Exception {
        return call();
    }
}